<?php

include ('inc/common.inc.php');

$systemName = "会员管理系统";
$copyright = "Network Collection Group";

if(isset($_POST[sub]))
{

	if(VerifyUsername($_POST['username']) && VerifyPassword($_POST['password1']) &&
	   VerifyStudentId($_POST['studentid']) && VerifyRealName($_POST['realname']) &&
	   VerifyEmail($_POST['email']) && VerifyGroupName($_POST['groupname']) &&
	   VerifyCommon($_POST['description']) && VerifyRealName($_POST['founder']))
	{
		//check password
		if($_POST['password1'] == $_POST['password2'])
		{
			$password = md5($password1);
		}
		else
		{
			message("网页消息","对不起，您所输入的密码不一致，请重新输入","groupregister.php");
			exit();
		}


		//insert operation
		$currentTime = mktime();
		$sql = "INSERT INTO `{$pre}groupinfo` (`id`,`groupname` ,`type` ,`founder` ,`school` ,`description` ,`time`)".
                " VALUES (NULL,'{$_POST['groupname']}', '{$_POST['type']}', '{$_POST['founder']}', '{$_POST[school]}', '{$_POST['description']}', '{$currentTime}')";
		if($db->query($sql))
		{

			$groupid = $db->insert_id();

			$sql = "INSERT INTO `{$pre}userinfo_basic` (`id` ,`groupid` ,`username` ,`password` ,`realname` ,`studentid` ,`email` ,`rightgroup` ,`lastlogintime` ,`time`)" .
					"VALUES (NULL,'{$groupid}', '{$_POST['username']}', '{$password}', '{$_POST['realname']}','{$_POST['studentid']}', '{$_POST['email']}', '1', '{$currentTime}', '{$currentTime}')";
			if($db->query($sql))
			{
				$userid = $db->insert_id();
				//create user infomation struct
				$sql = "INSERT INTO `{$pre}userinfo_struct` (`id` ,`groupid` ,`name` ,`type` ,`order`,`pattern`,`note`) ".
						"VALUES (NULL,  '{$groupid}',  '性别',  '4', '1', '男*女','请填写您的性别')";
				$db->query($sql);

				$sql = "INSERT INTO `{$pre}userinfo_struct` (`id` ,`groupid` ,`name` ,`type` ,`order`,`pattern`,`note`) ".
						"VALUES (NULL,  '{$groupid}',  '学院',  '1', '2', '','请填写您所在学院')";
				$db->query($sql);

				$sql = "INSERT INTO `{$pre}userinfo_struct` (`id` ,`groupid` ,`name` ,`type` ,`order`,`pattern`,`note`) ".
						"VALUES (NULL,  '{$groupid}',  '手机',  '1','3',  '/^\d{11}$/','请填写您的手机号，以便于我们飞信通知')";
				$db->query($sql);

				$sql = "INSERT INTO `{$pre}userinfo_struct` (`id` ,`groupid` ,`name` ,`type` ,`order`,`pattern`,`note`) ".
						"VALUES (NULL,  '{$groupid}',  'QQ',  '1', '4',  '/^\d{5,11}$/','请填写您的QQ号，以便于联系您')";
				$db->query($sql);

				$sql = "INSERT INTO `{$pre}userinfo_struct` (`id` ,`groupid` ,`name` ,`type` ,`order`,`pattern``note`) ".
						"VALUES (NULL,  '{$groupid}',  '个人简介',  '2', '5', '/^\d{11}$/','请填写您个人简介')";
				$db->query($sql);

				// create rights group

				$sql = "INSERT INTO  `{$pre}rights` (`id` ,`groupid` ,`type` ,`shell`)".
						"VALUES (NULL ,  '{$groupid}',  'admin',  '1000000000011111111111111111111')";
				$db->query($sql);

				$adminId = $db->insert_id();

				$sql = "INSERT INTO  `{$pre}rights` (`id` ,`groupid` ,`type` ,`shell`)".
						"VALUES (NULL ,  '{$groupid}',  'common',  '1000000000000000000001111111111')";
				$db->query($sql);

				// update the rightgroup number
				$sql = "UPDATE `{$pre}userinfo_basic` SET  `rightgroup` =  '{$adminId}' WHERE  `id` ={$userid}";
				$db->query($sql);
				message("网页消息","注册成功，三秒后重新定向到个人中心","test.php");
				exit();
			}

		}

	}
	else
	{
		message("网页消息","您所填写的信息有误，或者用户名已被注册，请重新填写", "groupregister.php");
		exit ();
	}
}

include template('groupregister.html');
?>
